package git.soulbgm.controller;

import git.soulbgm.pojo.User;
import git.soulbgm.util.Metaspace;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2019-01-30 17:03
 * @description 模拟内存溢出异常
 */
@RestController
public class MemoryController {

    private List<User> userList = new ArrayList<>();
    private List<Class<?>> classList = new ArrayList<>();

    /**
     * 堆内存溢出
     * 设置：-Xmx32M -Xms32M
     * 异常：Exception in thread "http-nio-8080-AsyncTimeout" java.lang.OutOfMemoryError: Java heap space
     */
    @RequestMapping("/heap")
    public void heap() {
        int i = 0;
        while (true) {
            userList.add(new User(i++, UUID.randomUUID().toString()));
        }
    }

    /**
     * 非堆内存溢出
     * 设置：-XX:MetaspaceSize=32M -XX:MaxMetaspaceSize=32M
     * 异常：Exception in thread "http-nio-8080-exec-1" java.lang.OutOfMemoryError: Metaspace
     */
    @RequestMapping("/nonheap")
    public void nonheap(){
        while (true) {
            classList.addAll(Metaspace.createClasses());
        }
    }

    // -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./
}
